#include "widget.h"
#include "ui_widget.h"
#include<QFileDialog>
#include<QVariant>
#include<QListWidget>
#include<QListWidgetItem>
#include<QUuid>
Widget::Widget(QTcpSocket* tso,QString name,QPixmap pm)
    :ui(new Ui::Widget)
{
    ui->setupUi(this);
    init(tso,name,pm);
    ConnectSingal();

}

Widget::~Widget()
{
    delete ui;
}

void Widget::init(QTcpSocket* tso,QString name,QPixmap pm)
{
    addpeople=new addPeople(this);
    addpeople->hide();
    mypm=pm;
    myname=name;
    ts=tso;
    ui->headimgbtn->setIcon(mypm);
    connect(ts,&QTcpSocket::connected,this,[=](){
        qDebug()<<"new Socket create";
    });

    connect(ts,&QTcpSocket::readyRead,this,[=](){
        QString str=ts->readAll();
        qDebug()<<"客户端:"<<myname<<"收到了服务端发来的消息:"<<str;
        QString mes;
        switch(ConfirmInfoType(str,&mes)){

        case ADDPEOPLE:
        {
            QString addedpeople;
            QString image;
            bool exist;
            GainAddPeopleMes(mes,&addedpeople,&exist,&image);
            if(exist)
                addPeopleItem(addedpeople,QPixmap(image));
        }
        break;
        case SENDMES:
        {
            QString sender,date_time,contend_type,image;
            QVariant contend;
            GainSendMes(mes,&sender,&date_time,&image,&contend_type,&contend);
            qDebug()<<"widget56"<<sender<<" "<<date_time<<" "<<contend_type<<" "<<image<<" "<<contend;
            QPixmap pm;pm.load(image);
            if(pm.isNull())
                qDebug()<<"图片数据为空";
            ReceiverPeopleMes(peopleitemsName[sender],contend_type,contend,date_time,pm);
        }
        break;
        default:
            break;
        }
    });
}

void Widget::addPeopleItem(QString name,QPixmap pm)
{
    PeopleItem* newpm=new PeopleItem(this,nextpeopleitem_row,name,pm);
    PeopleItem* newpm1=new PeopleItem(ui->peoListWidget,nextpeopleitem_row,name,pm);
    peopleitems[newpm->PeopleId]=newpm;
    peopleitemsSearch[nextpeopleitem_row]=newpm;
    peopleitemsName[name]=newpm;
    QListWidgetItem* li=new QListWidgetItem(ui->peoListWidget);
    li->setSizeHint(QSize(ui->peoListWidget->width(),50));
    ui->peoListWidget->setItemWidget(li,newpm1);
    if(cur_peopleid=="")
    {
        cur_peopleid="123";
        cur_item=newpm;
        qDebug()<<"widget80"<<"renew currentwidget";
        ui->curPeopleLable->setText(newpm->people_name);
    }

    nextpeopleitem_row++;
}

void Widget::ConnectSingal()
{
    connect(addpeople,&addPeople::setPeopleItem,[=](QString name){
        QString mes;
        SetAddpeopleForm(&mes,name);
        qDebug()<<"widget91:"<<mes;
        ts->write(mes.toUtf8());
    });
}

void Widget::DealMySendMes(QString str)
{
    if(str.size()==0)
        return;
    PeopleMes* pm=new PeopleMes(this,str,QTime::currentTime().toString(),mypm,true);
    PeopleMes* pm1=new PeopleMes(this,str,QTime::currentTime().toString(),mypm,true);
    cur_item->allpeoplemes.push_back(pm1);
    QListWidgetItem* li=new QListWidgetItem(ui->listWidget);
    li->setSizeHint(QSize(ui->listWidget->width()/2,50));
    ui->listWidget->setItemWidget(li,pm);

    //更新最后一条信息
    cur_item->DealLastMesRenew(str);

}

void Widget::ChangeCurPeopleItem(int row)
{
    cur_item=peopleitemsSearch[row];
    //清除所有消息
    ui->listWidget->clear();
    //修改标题
    ui->curPeopleLable->setText(cur_item->people_name);
    ui->curPeopleLable->repaint();
    //更细当前聊天对象的消息内容
    for(auto e:cur_item->allpeoplemes)
    {
        QListWidgetItem* li=new QListWidgetItem(ui->listWidget);
        li->setSizeHint(QSize(ui->listWidget->width()/2,100));
        PeopleMes* pm=new PeopleMes(ui->listWidget,e->content,e->datetime,e->pm,e->Isown);
        ui->listWidget->setItemWidget(li,pm);
    }
}

void Widget::ReceiverPeopleMes(PeopleItem *pi, QString contend_type,QVariant contend, QString time, QPixmap pm)
{

    if(contend.convert(QMetaType::QString)&&contend_type=="QString")
    {
        qDebug()<<"widget132"<<"to receiverPeopleMes";
        QString mes=contend.value<QString>();
        if(pm.isNull())
            qDebug()<<"widget141:pixmap nullptr";
        pi->AddOneMes(mes,pm,time,false);
    }

}




QString Widget::ProcessSendMes(QString mes)
{
    QString prefix=myname+" "+cur_item->people_name+" "+mes;
    qDebug()<<"prefix:"<<prefix;
    return prefix;
}

void Widget::SetAddpeopleForm(QString *mes, const QString &addedpeople)
{
    //infotype:addpeople;addingpeople:name;addedpeople:name;;   ---定义添加联系人格式
    *mes=QString("infotype:addpeople;addingpeople:%1;addedpeople:%2;").arg(myname).arg(addedpeople);
}

void Widget::SetSendMesForm(QString *mes, QString receiver, QString time, QString contend_type, QVariant contend)
{
    *mes=QString("infotype:sendmes;sender:%1;receiver:%2;date:%3;contend_type:%4;contend:%5;").arg(myname).arg(receiver).arg(time).arg(contend_type).arg(contend.toString());
}

Widget::InfoType Widget::ConfirmInfoType(const QString &str, QString *mes)
{
    int start=str.indexOf(":");
    int end=str.indexOf(";");
    QString infotype=str.sliced(start+1,end-start-1);
    *mes=str.sliced(end+1);
    if(infotype=="addpeople")
    {
        return ADDPEOPLE;
    }
    else if(infotype=="sendmes")
    {
        return SENDMES;
    }

}

void Widget::GainAddPeopleMes(QString str, QString *addedpeople, bool *exist, QString *image)
{
    int start=str.indexOf(":");
    int end=str.indexOf(";");
    QString exis=str.sliced(start+1,end-start-1);
    start=str.indexOf(":",start+1);
    end=str.indexOf(";",end+1);
    *addedpeople=str.sliced(start+1,end-start-1);
    if(exis=="yes")
        *exist=true;
    else
        *exist=false;
    if(*exist)
    {
        start=str.indexOf(":",start+1);
        end=str.indexOf(";",end+1);
        *image=str.sliced(start+1,end-start-1);
    }


}

void Widget::GainSendMes(QString str, QString *sender, QString *date_time, QString *image, QString *contend_type, QVariant *contend)
{
    int start=str.indexOf(":");
    int end=str.indexOf(";");
    *sender=str.sliced(start+1,end-start-1);
    start=str.indexOf(":",end+1);
    end=str.indexOf(";",end+1);
    *date_time=str.sliced(start+1,end-start-1);
    start=str.indexOf(":",end+1);
    end=str.indexOf(";",end+1);
    *image=str.sliced(start+1,end-start-1);
    start=str.indexOf(":",end+1);
    end=str.indexOf(";",end+1);
    *contend_type=str.sliced(start+1,end-start-1);
    start=str.indexOf(":",end+1);
    end=str.indexOf(";",end+1);
    *contend=str.sliced(start+1,end-start-1);
}





void Widget::on_mesbtn_clicked()
{
}


void Widget::on_addPeoBtn_2_clicked()
{
    addpeople->show();
    addpeople->setGeometry(ui->addPeoBtn_2->x()+30,ui->addPeoBtn_2->y()+30,300,300);
}


void Widget::on_pushButton_clicked()
{
    QString mes=ui->sendMesEdit->toPlainText();
    DealMySendMes(mes);
    ui->sendMesEdit->clear();
    QString sendmes;
    SetSendMesForm(&sendmes,cur_item->people_name,QTime::currentTime().toString(),"QString",mes);
    qDebug()<<"客户端"<<myname<<"send"<<sendmes;
    ts->write(sendmes.toUtf8());
}



void Widget::on_peoListWidget_currentRowChanged(int currentRow)
{
    ChangeCurPeopleItem(currentRow);
}

